package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnCheckBox;
import ar.com.mooral.crohn.view.CrohnImageView;
import ar.com.mooral.data.Consultations;
import ar.com.mooral.data.Depositions;
import ar.com.mooral.data.FoodList;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.MedicationsList;
import ar.com.mooral.data.SexualActivityIncidence;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.ConsultationPojo;
import ar.com.mooral.pojo.MedicationPojo;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class DoctorConsultations extends CrohnMenuActivity {
	
	private Vector<CrohnImageView> deleteImageViews;

	protected void onStart() {
		super.onStart();

        // DELETE
        ((Button)findViewById(R.id.btnDoctorConsultationsDelete)).setOnClickListener(new View.OnClickListener() {
		    public void onClick(View view) {
		    	for (ImageView img : deleteImageViews ) {
		    		img.setVisibility((img.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE));
		        }
		    }
        });

        
        // AGREGO LAS CONSULTAS
        LinearLayout linearLayoutDoctorConsultations = (LinearLayout)findViewById(R.id.linearLayoutDoctorConsultations);
        linearLayoutDoctorConsultations.removeAllViews();
        
        Consultations consultations = new Consultations(this);
        Vector<Hashtable<String, String>> datas = (Vector<Hashtable<String, String>>)consultations.findAll();
        
        Collections.sort(((Vector)datas), new PojoComparatorName());
        deleteImageViews = new Vector<CrohnImageView>();

        for(Hashtable<String,String> data: datas){
        	ConsultationPojo pojo = (ConsultationPojo)data;
        	
        	
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setBackgroundResource(R.drawable.medication_bg);
			ll.setGravity(Gravity.CENTER_HORIZONTAL);
			/*
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setPadding(5, 5, 5, 5);
			ll.setGravity(Gravity.CENTER_VERTICAL);
			*/
			linearLayoutDoctorConsultations.addView(ll);			

			CrohnImageView deleteImageView = new CrohnImageView(this);
			deleteImageView.setBackgroundResource(R.drawable.stop);
			deleteImageView.setLayoutParams(new LayoutParams(20, 20));
			deleteImageView.setVisibility(View.GONE);
			deleteImageView.setPojo(pojo);
			deleteImageView.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnImageView) view).getPojo();
    		    	basePojo.getDatabaseHelper().delete(basePojo.getId());
    		    	((LinearLayout) view.getParent().getParent()).removeView((View) view.getParent());
    		    }
            });

			/*
			deleteImageView.setBackgroundResource(R.drawable.stop);
			deleteImageView.setLayoutParams(new LayoutParams(25, 25));
			deleteImageView.setVisibility(View.GONE);
			deleteImageView.setPojo(pojo);
			deleteImageView.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnImageView) view).getPojo();
    		    	basePojo.getDatabaseHelper().delete(basePojo.getId());
    		    	((LinearLayout) view.getParent().getParent()).removeView((View) view.getParent());
    		    }
            });
            */
			deleteImageViews.add(deleteImageView);
			ll.addView(deleteImageView);
			
			
			CrohnButton recordButton = new CrohnButton(this);
			//recordButton.setText(record.get("nombre"));
			
			String text = pojo.getName();
			if(text.length()>30) text = text.substring(0,30) + "...";
			recordButton.setText(text);
			recordButton.setLayoutParams(new LayoutParams(215, 30));
			recordButton.setBackgroundResource(R.drawable.white);
			recordButton.setTextSize(10);
			recordButton.setPojo(pojo);
			/*	
			//Button recordButton = new Button(this);
			CrohnButton recordButton = new CrohnButton(this);
			String text = pojo.getName();
			if(text.length()>30) text = text.substring(0,30) + "...";
			recordButton.setText(text);
			recordButton.setLayoutParams(new LayoutParams(245, LayoutParams.WRAP_CONTENT));
			recordButton.setTextSize(10);
			recordButton.setPojo(pojo);
			*/
			recordButton.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnButton) view).getPojo();
		    		CrohnContext.setPojo(basePojo);
    		    	
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(	DoctorConsultationNew.class.getPackage().getName(), 
							DoctorConsultationNew.class.getName()));
					startActivity(intent);
    		    }
            });
			ll.addView(recordButton);
			
        }

        
		
	}

	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_consultations);
        
		// CANCEL
        Button btnDoctorConsultationsCancel = (Button) findViewById(R.id.btnDoctorConsultationsCancel);
        btnDoctorConsultationsCancel.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});		

		// ADD
        Button btnDoctorConsultationsNew = (Button) findViewById(R.id.btnDoctorConsultationsNew);
        btnDoctorConsultationsNew.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				CrohnContext.setPojo(new ConsultationPojo());
				intent.setComponent(new ComponentName(	DoctorConsultationNew.class.getPackage().getName(), 
														DoctorConsultationNew.class.getName()));
				//startActivity(intent);
         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
         		parentActivity.startChildActivity("DoctorConsultationNew", intent);

		    }
		});

    }
}
